﻿@model Citi.HeadCount.WebUI.Models.TypeRuleResultViewModel
<script type="text/javascript">
    $(function () {
        $("#typeResultValues").change(function () {
            $("#TypeResultId").val($("#typeResultValues :selected").val());
        });

        $("#AddTypeRule").click(function () {
            var error = false;
            $("#tableTypeRules").find('tr').each(function (index, item) {
                if (item.cells[0].innerHTML == $("#TypeRuleTypes :selected").text()
                        && item.cells[1].innerHTML == $("#typeResultValues :selected").text()) {
                    error = true;
                }
            });

            if (error == true) return false;

            $("#formTypeRules").submit();
        });

        $("#TypeRuleTypes").change(function () {
            $.ajax({
                url: '@Url.Action("GetRuleResultByType", "TypeRuleResult")',
                type: 'POST',
                assync: false,
                data: { type: $("#TypeRuleTypes :selected").val() },
                success: function (json) {
                    var items = '';

                    items += '<option value="-1">-</option>';
                    $.each(json, function (i, item) {
                        items += '<option value="' + item.Id + '">' + item.Value + '</option>';
                    });

                    $("#typeResultValues").html(items);

                    //                    if (selecionado != undefined && selecionado != null)
                    //                        $("#" + idValorEnums).val(selecionado);
                },
                error: function (a, b) {
                    alert("Error");
                }
            });
        });
    });

    function cleartypeResultValues() {
        $("#typeResultValues").empty();
    }
</script>
@using (Ajax.BeginForm("Create", "TypeRuleResult", new AjaxOptions { UpdateTargetId = "typeRules", 
    OnComplete = "clearAllContentExceptHidden", OnSuccess = "cleartypeResultValues" }, new { id = "formTypeRules" }))
{
    <fieldset>
        <legend>Create Result Rule</legend>
        <br />
        @Html.HiddenFor(model => model.TypeResultId)
        @Html.HiddenFor(model => Model.RuleResultId, new { @class = "hiddenRuleResultId" })
        
        @Html.LabelFor(model => model.TypeResultId)
        @Html.DropDownList("TypeRuleTypes")
        
        @Html.LabelFor(model => model.RuleResultId)
        <select id="typeResultValues" style="width:180px" />
        
        <input type="button" value="Add" id="AddTypeRule" style="margin-left:10px" />
    </fieldset>
}
